<template>
    <div class="input_content">
        <input @input="input($event)" v-rules="displayErrors" :data-rules="rules" @blur="blur($event)"
            @focus="focus($event)" :placeholder="text" type="text">
        <div class="input_border_top"></div>
        <div class="input_border_bottom"></div>
        <div class="input_border_left"></div>
        <div class="input_border_right"></div>
    </div>
    <div class="input_error">
        <span>{{ error ? "* " + error : '' }}</span>
    </div>
</template>
<script setup>
import { reactive, ref, defineEmits } from 'vue';
const emit = defineEmits(['input', 'error'])
let error = ref('');
function displayErrors(text) {
    error.value = text;
    emit('error', text)
}
function input(e) {
    emit('input', e.target.value)
}
displayErrors()
const props = defineProps(
    {
        text: {
            default: () => ''
        },
        rules: {
            default: () => ''
        },
    }
)
function focus(e) {
    e.target.parentNode.querySelector('.input_border_top').style = 'width:100%;'
    e.target.parentNode.querySelector('.input_border_bottom').style = 'width:100%;'
    e.target.parentNode.querySelector('.input_border_left').style = 'height:100%;'
    e.target.parentNode.querySelector('.input_border_right').style = 'height:100%;'
}
function blur(e) {
    let arr = e.target.parentNode.querySelectorAll('.input_content>div')
    arr.forEach(item => {
        item.style = ''
    })
}
</script>
<style scoped lang="scss">
@function r($v) {
    @return $v / (390 /10rem)
}

.input_error {
    font-size: r(10);
    color: red;
    display: flex;
    align-items: center;
    padding-left: r(10);
    margin-top: r(5);
    margin-bottom: r(5);
    height: r(10);
    position: relative;
}

.input_content {
    width: 100%;
    height: r(44);
    position: relative;

    input {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        padding-left: r(15);
        background-color: #f8f8f8;
        box-sizing: border-box;
        font-size: r(14);
        color: #222;
        border-radius: r(5);
    }

    input::placeholder {
        font-size: r(14);
        color: #999;
    }

    div {
        transition: all 0.5s;
        position: absolute;
    }

    .input_border_top {
        top: 0;
        left: 0;
        height: r(1);
        background: linear-gradient(to right, #000, rgba(255, 255, 255, 0));
        width: 0;
    }

    .input_border_bottom {
        right: 0;
        bottom: 0;
        height: r(1);
        background: linear-gradient(to left, #000, rgba(255, 255, 255, 0));
        width: 0;
    }

    .input_border_left {
        top: 0;
        left: 0;
        width: r(1);
        height: 0;
        background: linear-gradient(to bottom, #000, rgba(255, 255, 255, 0));
    }

    .input_border_right {
        bottom: 0;
        right: 0;
        height: 0;
        width: r(1);
        background: linear-gradient(to top, #000, rgba(255, 255, 255, 0));
    }
}
</style>